package com.chenwang.measure.utils;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.chenwang.measure.MyApp;
import com.chenwang.measure.model.bean.LocationBean;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.util.Set;
//蓝牙工具类
public class BluetoothUtil {





    //启动SBAs模式
    public static void openSBAS(){
        try {
            OutputStream outStream = MyApp.getBluetoothSocket().getOutputStream();
            outStream.write("sbascontrol enable msas\r\n".getBytes());
            outStream.write("rtkobsmode 3\r\n".getBytes());
            outStream.write("saveconfig\r\n".getBytes());

        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    public static void  closeSBAS(){
        try {
            OutputStream outStream = MyApp.getBluetoothSocket().getOutputStream();
            outStream.write("sbascontrol disable msas\r\n".getBytes());
            outStream.write("rtkobsmode 0\r\n".getBytes());
            //outStream.write("saveconfig\r\n".getBytes());



        } catch (IOException e) {
            MyCrashHandler.newInstance().saveCrashInfoToFile(e);
        }
    }
    //打开电台模式
    public static void openRadioBroadcasting(){
        try {
            OutputStream outStream = MyApp.getBluetoothSocket().getOutputStream();
           // outStream.write("unlogall\r\n".getBytes());
            outStream.write("log com3 bestposb ontime 1\r\n".getBytes());
            outStream.write("interfacemode com2 auto auto on\r\n".getBytes());
            outStream.write("$$0000A4600500132*46\r\n".getBytes());
           // outStream.write("saveconfig\r\n".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }




    //关闭电台模式
    public static void closeRadioBroadcasting(){
        try {
            OutputStream outStream = MyApp.getBluetoothSocket().getOutputStream();
            //outStream.write("unlogall\r\n".getBytes());
           // outStream.write("log com3 bestposb ontime 1\r\n".getBytes());
            outStream.write("interfacemode com2 compass on\r\n".getBytes());
            outStream.write("saveconfig\r\n".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //根据地址查询 返回已配对的蓝牙设备
    public static BluetoothDevice queryBluetooth(String mac){
        BluetoothDevice device = null;
        Set<BluetoothDevice> devices = BluetoothAdapter.getDefaultAdapter().getBondedDevices();
        for (BluetoothDevice d :devices ) {
            if (d.getAddress().equals(mac)){
                device = d;
            }
        }
        return device;
    }

    //设备是否配对连接过
    public static boolean isBluetoothconnect(Context context){
        String mac = getMac(context);
        if (TextUtils.isEmpty(mac)){
            return false;
        }else {
            if (queryBluetooth(mac) != null){
                return true;
            }else {
                return false;
            }
        }
    }

    public static String getMac(Context context){
        String mac = SharePreferenceUtils.getString(context,"bluetooth_mac" ,"" );
        if (TextUtils.isEmpty(mac)){
            return null;
        }else {
            return mac;
        }
    }


/*    public static String read
   ByteArrayOutputStream result = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length;
while ((length = inputStream.read(buffer)) != -1) {
        result.write(buffer, 0, length);
    }
return result.toString("UTF-8");*/




}
